package component;

import java.util.ArrayList;

import basicTypes.UserRecord;
import Prism.core.AbstractImplementation;
import Prism.core.Event;
import Prism.core.PrismConstants;

// This is OfflineDatabase Component's implementation
public class OffLineDatabase extends AbstractImplementation{
	// prov Name(Event Name)
	public static final String provGetBills = "getBills";

    //billing: \set OffLineRecordType;
	ArrayList<UserRecord> billing;

	@Override
	public void start() {
		super.start();
		
		billing = new ArrayList<UserRecord>();
		
		//Test Bill
		billing.add(new UserRecord("25000","Kwan Park","100","2132548799"));
		billing.add(new UserRecord("23300","USC","5534","1231231231"));
		billing.add(new UserRecord("21200","CSCI","1002","2342342342"));
		billing.add(new UserRecord("24400","NENO","344","3453453453"));
	}
	
	// Check invariant property
	public boolean checkInvariant() {
		// check bills size
		if(billing.size()>=0) return true;
		return false;
	}
	
	@Override
	public void handle(Event e) {
		// Handle of request of provGetBills
		if (e.getName().equals(provGetBills)) {

			Event n;
			
			int size = billing.size();
			for(int i=0; i<size; i++) {
				n = new Event(provGetBills, PrismConstants.REPLY);
				n.addParameter("name", billing.get(i).getName());
				n.addParameter("phoneNumber", billing.get(i).getPhoneNumber());
				n.addParameter("amount", billing.get(i).getAmount());
				n.addParameter("time", billing.get(i).getTime());
				n.addParameter("clearFlag", (i==0)?"true":"false");
				this.send(n);
			}

		}
		
	}

}
